package com.operation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.util.Log4gUtil;
public class HttpSendUrl {
	/**
	 * 发送请求
	 * @param url 地址
	 * @param param 参数
	 * @param requestMethod 方式
	 * @param setConnectTimeout 响应时间
	 * @return 
	 */
	public String getSendRequest(String url,String param,String requestMethod,int setConnectTimeout){
		Log4gUtil.getLog().info("getSendRequest-进入:"+url+"?"+param);
		String datas ="";
			if(setConnectTimeout<100)
				setConnectTimeout=5000;
			
			URL u;
			BufferedReader br = null;
			
			try {
				u = new URL(url);
			
			HttpURLConnection httpConn = (HttpURLConnection) u.openConnection();  
			httpConn.setConnectTimeout(setConnectTimeout);
			httpConn.setReadTimeout(setConnectTimeout); 
				//httpConn.setRequestProperty("Pragma:", "no-cache");  
				//httpConn.setRequestProperty("Cache-Control", "no-cache");
				//httpConn.setRequestProperty("Content-Type", ContentType.getType(cte)); 
			//输出流
			if((requestMethod.equals("POST")||requestMethod.equals("post"))&&param!=null&&!param.equals("")){
				//设置请求属性
				httpConn.setDoOutput(true); 
				httpConn.setRequestProperty("Pragma:", "no-cache");  
				httpConn.setRequestProperty("Cache-Control", "no-cache");
			    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			    httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
			    httpConn.setRequestProperty("Charset", "utf-8");
				OutputStreamWriter URLout = new OutputStreamWriter(httpConn  
	                    .getOutputStream(),"utf-8");  
				URLout.write(param);
				URLout.flush();  
		        URLout.close(); 
	        }
			
			Log4gUtil.getLog().info("getSendRequest--发送url:"+url+"?"+param);
	        //接收数据
			br = new BufferedReader(new InputStreamReader(
					httpConn.getInputStream(), "utf-8"));
			StringBuffer sb1 = new StringBuffer();
			String line = null;
			while ((line = br.readLine()) != null) {
				sb1.append(line);
			}
			 datas = sb1.toString();
			 br.close();
			 httpConn.disconnect();
			}catch(SocketTimeoutException e){
				Log4gUtil.getLog().info("getSendRequest-超时"+url+"?"+param);
				datas = "****错误-连接超时";
			} catch (MalformedURLException e) {
				Log4gUtil.getLog().info("getSendRequest-url格式错误"+url+"?"+param);
				datas = "****错误-url格式错误";
			} catch (IOException e) {
				Log4gUtil.getLog().info("getSendRequest-流异常"+url+"?"+param);
				datas = "****错误-流异常";
			} 
			Log4gUtil.getLog().info("getSendRequest-接收-:"+datas+"-"+url+"?"+param);
			return datas;
		}
	}
